class Advert < ActiveRecord::Base
  has_many :campaign_adverts, :dependent => :destroy
  has_attached_file :custom_banner_image, :styles =>{ :medium => "85x100>"}
  attr_accessible :name, :advertiser_name, :max_views, 
  :view_count,
  :is_active,
  :is_iframe,:iframe_src,
  :is_script,:script_content,
  :is_image_banner,:image_banner_src,:image_banner_href,
  :is_custom_banner,
  :custom_banner_image,:custom_banner_href,
  :width,:height
  
  
  validates_presence_of :name, :advertiser_name 
  validates_uniqueness_of :name
  validate :is_consistent
  
  
  def user
    User.find(self.user_id)
  end
  
  private
  def is_consistent
    if !( self.is_iframe || self.is_script || self.is_image_banner || self.is_custom_banner ) 
      errors.add(:is_iframe, "At least: Is script, is Iframe, is Image banner or is Custom_Banner")
    end
    
    if ( (self.is_iframe ? 1:0) +(self.is_script ? 1:0) + (self.is_image_banner ? 1:0) + (self.is_custom_banner ? 1:0) ) != 1
      errors.add(:is_iframe, "At least: Is script, is Iframe, is Image banner or is Custom_Banner")
    end
    
  end
  
  
end
